package com.bsj.power.common.util;


import cn.hutool.core.util.HexUtil;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 数据处理工具类
 *
 * @author 杨小华
 * @date 2019-10-23 21:07
 **/
public class DataUtil {
    /**
     * 横线
     */
    private final static String striping = "-";

    /**
     * 分页
     *
     * @param list
     * @param pageSize
     * @param pageNumber
     * @param <T>
     * @return
     */
    public static <T> List<T> pagingByList(List<T> list, int pageSize, int pageNumber) {
        if (list == null || list.isEmpty() || pageSize <= 0 || pageNumber <= 0) {
            return list;
        } else {
            int size = list.size();
            // 开始索引
            int startIndex = (pageNumber - 1) * pageSize;
            // 结束索引
            int endIndex = startIndex + pageSize;
            if (startIndex > size) {
                return list;
            } else {
                if (endIndex > size) {
                    endIndex = size;
                }
                return list.subList(startIndex, endIndex);
            }
        }
    }

    /**
     * Object 强制转化为List
     *
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }

    /**
     * 是否在线
     *
     * @param time
     * @return
     */
    public static boolean isOnLine(long time) {
        long systemTime = System.currentTimeMillis();

        // 系统时间减终端时间的差值，单位毫秒
        long diff = systemTime - time;
        if (diff <= 15 * 60 * 1000) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 数据分组，用于数据量大的情况。
     *
     * @param list 数据集合
     * @param num  一组多少个数据
     * @return
     */
    public static <T> List<List<T>> splitData(List<T> list, int num) {
        List<List<T>> lists = new ArrayList<List<T>>();
        if (list == null || list.isEmpty()) {
            return lists;
        }

        // 一组数据个数小于等于0，那么默认100个一组。
        if (num <= 0) {
            num = 100;
        }

        int size = list.size(); // 总数
        int count = size / num; // 分组数
        if (size <= num) {
            lists.add(list);
        } else {
            List<T> subList = new LinkedList<T>();
            for (int i = 0; i <= count; i++) {
                if (i == count) {
                    subList = list.subList(i * num, size);
                } else {
                    subList = list.subList(i * num, num * (i + 1));
                }
                if (!subList.isEmpty()) {
                    lists.add(subList);
                }
            }
        }
        return lists;
    }

    public static String[] lengthSplitStr(String src, int length) {
        int srcLen = src.length();
        if (length > srcLen) {
            return new String[]{src};
        }
        int n = (src.length() + length - 1) / length;
        String[] split = new String[n];
        for (int i = 0; i < n; i++) {
            if (i < (n - 1)) {
                split[i] = src.substring(i * length, (i + 1) * length);
            } else {
                split[i] = src.substring(i * length);
            }
        }
        return split;
    }

    /**
     * @功能: 10进制串转为BCD码
     * @参数: 10进制串
     * @结果: 长度
     */
    public static void str2Bcd(byte[] data, int offset, String asc) {
        int len = asc.length();
        int mod = len % 2;
        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }
        if (len >= 2) {
            len = len / 2;
        }
        byte[] abt = asc.getBytes();
        int j, k;
        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }
            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }
            int a = (j << 4) + k;
            byte b = (byte) a;
            data[offset++] = b;
        }
    }

    /**
     * 过滤特殊符号
     *
     * @param value
     * @return java.lang.String
     */
    public static String escape(String value) {
        if (null != value && (value.contains("_") || value.contains("%") || value.contains("#"))) {
            char[] chars = value.toCharArray();
            StringBuilder result = new StringBuilder();
            for (char ch : chars) {
                if (ch == '_' || ch == '%' || ch == '#') {
                    result.append("\\");
                }
                result.append(ch);
            }
            return result.toString();
        }
        return value;
    }

    /**
     * 数据分包
     * @param data 总数据
     * @param packetSize 每包数据大小
     * @return
     */
    public static byte[][] divideData(byte[] data, int packetSize) {
        int numPackets = (int) Math.ceil((double) data.length / packetSize);
        byte[][] packets = new byte[numPackets][];
        for (int i = 0; i < numPackets; i++) {
            int start = i * packetSize;
            int length = Math.min(packetSize, data.length - start);
            byte[] packet = new byte[length];
            System.arraycopy(data, start, packet, 0, length);
            packets[i] = packet;
        }
        return packets;
    }

    public static void main(String[] args) {
        String s = "A607DFA10D269D1194FFED1733B95F001A2831282427BC2F7D319DFFA60903008F5897D3002D3FC2819430D10C94FCB4D07B839B044041E73FE1ADE613B528FF62405C1E213801523EC8F400000002000005001ACA6DD3003A4090040933434F41542D373035000832303136303230310D53494E4F474E5353204B3830330B36323046312D312E303031083039333431363230DD186BD300133ED004033A6AEC369B8C852EB4AB45B12D313E6A20D1D30040412507A81FD9045EEC0000261A85DB710FEE7450705EA634EDF9328020961A026BF4A2A17362F76FFE60AE56156BFFEA27EB9534144A8116F8A5BFECC44791E4DBD286D3003E41515480176BEE845DC00001D413A17933ABC48C2C0874A3EFBD940046B3202E06A813A8285DC0030444C7288FFB5273217D31BC63E58FA1FFFBFFC00400D72142D3003F41679480216B14B864003FEA3BFCA792E82788A0BEB39AA72C1F0C00A49E742542A813BCAC640FFED99F2D7C30017273C23791E8E1CAC7FFCFFBEB2FE3F600F7E7CFD3008643200417471B4200200429218080000000202041005457447C2927A7A622A32227B8360DAC16BF08ACD1F0A4E238E24EA3F2A011A074216C42A994A212B9CDA3A78F515EC09D51833B8E12DD7428187FFBE7C06C1DFE464FA8AF4FFB65427E100418C3FE3F2E2E457F5EA1FC29BBEBDAB98CEDFFFFFFFFFE000113D053054B431C954F65AF803B546BD3005843C004253FC0020020300E000000000000204000007F69E8EA08C8C209F4B364113E386CE7826BC4471A243A7A69776556EC8F045B45911A04E1DC113A30CDFD41BFFEC21513E01EEF9F937EBFF7BFFC0133B23F13F45730E3BB7ED3009B44600417471B4200203085004020000000208101007FC447FAC2A28A927ADACE89CFA2175A5CEDD35FCD7B10F872F353A41751B6CC9DCBF9EA390964F939DE756CFFF2165D659C563A57F63EFB185C9B72CFFC5DC058A2F8DDA3CF175F51977D51FCF3D603E3A3CF1AEE4BACB00EAC48475D0118CA3DDD75EFC5281586FF094473FFFBFFFFFF33FFFFFC0000414574CB1512C91C72450BEFCB3500A592C4D300C24640041746408200205AC489060000000020820101721C83210847718BE3C403CBCBE3CBC27A928A8C768923E95BD88DEC12E0DA355DEBB1959359C69AF7088B9619F835A072F6D6FDA6E2D671AB1652DA23A438AD195A62E6046BFBEF7457F4E9DB002F59D0B3C2439EDDF8E350D22AAEB41E7D2193069A07D8E6EF5CC5015AF6F244A00F954F6616BD18911F21343B7FC2D9D64320F5FB7887E7D29FA4A07E1C4CFFFFFFFFFFFFFFFFFFFDEFF0000017596C93CB3C134F0C72471D93D354104508754311D3008D4640041746408000200000000087000018208201016F7B8E209C96F0F0F6F8C5E8EAC8406361614DC37C0847";
//        String s = "A607DFA10D269D";
        byte[][] bytes = divideData(HexUtil.decodeHex(s), 57);
        System.out.println(1);
    }
}
